package com.rz.htk.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rz.core.exception.GlobalException;
import com.rz.htk.order.entity.Commodity;
import com.rz.htk.order.entity.vo.OrderDeleteParam;
import com.rz.htk.order.mapper.CommodityMapper;
import com.rz.htk.order.service.CommodityService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CommodityServiceImpl extends ServiceImpl<CommodityMapper,Commodity> implements CommodityService{

    @Override
    public IPage<Commodity> getPage(Commodity commodity) {
        QueryWrapper wrapper = new QueryWrapper();
        if (StringUtils.isNotEmpty(commodity.getCommodityCode())) {
            wrapper.like("", commodity.getCommodityCode());
        }
        Page<Commodity> page = new Page<>(commodity.getPageNo(), commodity.getPageSize());
        IPage<Commodity> desktopIPage = page(page, wrapper);
        return desktopIPage;
    }

    public  List<Commodity> listCommodityByCode (String code){
        QueryWrapper<Commodity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("commodity_code", code);
        IPage<Commodity> iPage = new Page<>();
        return page(iPage, queryWrapper).getRecords();
    }

    @Transactional
    public boolean saveCommodity(Commodity commodity){
        if (commodity.getId() == null) {
            commodity.setCreationTime(LocalDateTime.now());
            commodity.setDelFlag(0);
        }
        boolean isOk = this.saveOrUpdate(commodity);
        if (!isOk) {
            throw new GlobalException("物品信息保存失败！");
        }
        return isOk;
    }

    @Override
    @Transactional
    public boolean batchDelete(OrderDeleteParam deleteParam){
        deleteParam.getIds().forEach(id -> {
            Commodity commodity = this.getById(id);
            if (commodity != null) {
                commodity.setDelFlag(1);
                boolean isOk = this.saveOrUpdate(commodity);
                if (!isOk) {
                    throw new GlobalException("【" + commodity.getCommodityCode() + "】信息删除失败！");
                }
            } else {
                throw new GlobalException("1信息不存在，删除失败！");
            }
        });
        return true;
    }
    @Override
    @Transactional
    public boolean deleteByCode(String code){
        List<Commodity> listCommodityByCode = this.listCommodityByCode(code);
        listCommodityByCode.forEach(commodity -> {
            this.removeById(commodity.getId());
        });
        return true;
    }

    public  List<Commodity> getListCommodityBycode (String code){
        QueryWrapper<Commodity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("commodity_code", code);
        queryWrapper.eq("del_flag", 0);
        return list(queryWrapper);
    }

    @Override
    public List<Commodity> getListCommodityBycodes(List<String> codes) {
        QueryWrapper<Commodity> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("commodity_code", codes);
        queryWrapper.eq("del_flag", 0);
        return list(queryWrapper);
    }

}

